﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace genshin_gacha
{
    internal class CharacterPrService
    {
        //private object result;

        public CharacterPrService() { 
        }

        public double BasePR(int i)
        {
            return  i==0?0: Math.Min((double)(i < 74 ? (double)0.006 : BasePR(i - 1) + (double)0.06),1);
        }

        public double NotGetPR(int i)
        {
            return i == 0 ? 1 : NotGetPR(i - 1) * (1 - BasePR(i));
        }
        /// <summary>
        /// 保底抽数
        /// </summary>
        public int Limit { get; private set; } = 90;
        /// <summary>
        /// 打算抽几个UP
        /// </summary>
        public int GachaCount { get; set; } = 1;
        /// <summary>
        /// UP不外概率
        /// </summary>
        public double WinPR { get; set; } = (double)0.50;
        private Dictionary<string, double> _cache = new Dictionary<string, double>();
        public void ClearCache()
        {
            _cache.Clear();
        }
        public double NotGetPR(int i, int j, int x)
        {
            string key = $"{i}-{j}-{x}";
            if (_cache.ContainsKey(key))
            {
                return _cache[key];
            }
            double val = 0d;
            if (i==0&&j==0 && x==0)
            {
                val = 1;
            }
            else if(x==1 && j == 0)
            {
                val =  NotGetPR(i - 1, i - 1, 0) * BasePR(i) * (1- WinPR);
            }
            else
            {
                val = NotGetPR(i - 1, j - 1, x) * (1 - BasePR(j));
                
            }
            return _cache[key]=val;
        }
        public double GetPR(int i)
        {
            return NotGetPR(i-1)*BasePR(i);
        }

        public double GetUpPR(int i)
        {
            //Console.WriteLine("------------------");
            if (i <= Limit)
            {
                var a =  NotGetPR(i - 1, i - 1, 0) * BasePR(i) * WinPR;      //没有获得五星的概率
                //Console.WriteLine($"total {i} gacha: small PR= {a * 100:0.########}%");
                var b =  Enumerable.Range(1, Math.Min(i-1, Limit)).Select(j => NotGetPR(i - 1, j - 1, 1) * BasePR(j)).Sum();  //获得UP五星概率
                //Console.WriteLine($"total {i} gacha :PR={a * 100:0.########}% + {b * 100:0.########}% = {(a + b) * 100:0.########}%");
                return a + b;
            }
            else
            {
                var a = Enumerable.Range(1, Limit).Select(j => NotGetPR(i - 1, j - 1, 1) * BasePR(j)).Sum();
                //Console.WriteLine($"total {i} gacha :{a * 100:0.################}%");
                return a;
            }
            

        }
    }
}
